package com.example.controller;
import com.example.pojo.Result;
import com.example.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
    //本地存储文件
//    @PostMapping("/upload")
//    public Result upload(String username , Integer age , MultipartFile image) throws Exception {
//       log.info("文件上传{},{},{}",username,age,image);
//       //获取原始文件名
//        String originalFilename = image.getOriginalFilename();
//        //构造唯一文件名(uuid --通用唯一识别码 不可重复)
//        int index = originalFilename.lastIndexOf(".");
//        //获取文件拓展名
//        String exname = originalFilename.substring(index);
//        String NewFileName = UUID.randomUUID().toString()+exname;
//        log.info(NewFileName);
//
//
//        //将文件存储到本地目录 E:\images
//      image.transferTo(new File("E:\\images\\"+NewFileName));
//       return Result.success();
//    }
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传,原始名{}",image.getOriginalFilename());
        //调用阿里云oss工具类进行文件上传
         String url = aliOSSUtils.upload(image);
         log.info("文件上传完成，文件访问的url为{}",url);

         return Result.success(url);
    }

}
